package com.sjq.common.web;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.sjq.common.entity.Role;
import com.sjq.common.service.RoleService;
import com.sjq.common.util.PageResults;

@Controller
public class RoleController {
	
	@Autowired
    private RoleService roleService;
	
	private static final String INITURL = "sys-manage/role-manage";
	
	//初始化
    @RequestMapping(INITURL)
    public ModelAndView init(Integer pageNo, Integer pageSize, Role role){
    	if(role==null){
    		role=new Role();
    	}
    	pageNo = pageNo == null ? 1 : pageNo;
        PageResults<?> pageResults=roleService.findRoleByPage(pageNo, PageResults.ALLPAGESIZE, role);
    	ModelAndView mav = new ModelAndView();
        mav.setViewName("/view/role/roleList");
        mav.addObject("allRole", pageResults.getResults());
        mav.addObject("pageNo", pageResults.getPageNo());
        mav.addObject("pages", pageResults.getPageCount());
        return mav;
    }
    
    //跳转新增或修改
    @RequestMapping("/roleModify")
    public @ResponseBody ModelAndView showRoleModify(@RequestParam("roleId") String roleId) throws Exception{
    	if(roleId.equals("-1")){
    		return new ModelAndView("view/role/roleModify");
    	}else{
    		int js_id=Integer.parseInt(roleId);
    		Map<String, Object> role=roleService.findSingleRole(js_id);
    		ModelAndView mav=new ModelAndView();
			mav.setViewName("view/role/roleModify");
			mav.addObject("role",role);
			return mav;
    	}
    }
    
    //新增角色
    @RequestMapping("/AddRole")
    public  ModelAndView addRoleInfo(@RequestParam("js_id") String js_id,@RequestParam("jsmc") String jsmc,@RequestParam("jssx") String jssx) throws Exception{
    		Role role = new Role();
    		role.setJS_ID(js_id);
    		role.setJSMC(jsmc);
    		role.setJSSX(jssx);
    		roleService.addRole(role);
    		ModelAndView mav=new ModelAndView("view/role/roleModify");
			return mav;
    }
    //修改角色
    @RequestMapping("/updateRole")
    public  ModelAndView updateRoleInfo(@RequestParam("js_id") String js_id,@RequestParam("jsmc") String jsmc) throws Exception{
	    	Role role = new Role();
			role.setJS_ID(js_id);
			role.setJSMC(jsmc);
			roleService.modifyRole(role);
    		ModelAndView mav=new ModelAndView("view/role/roleList");
			return mav;
    }
  //删除角色
    @RequestMapping("/delRole")
    public  ModelAndView delRoleInfo(@RequestParam("js_id") String js_id ) throws Exception{
    		int jsid=Integer.parseInt(js_id);
    		roleService.removeRole(jsid);
    		ModelAndView mav=new ModelAndView("view/role/roleList");
			return mav;
    }
    //跳转详细
    @RequestMapping("/roleDetails")
    public ModelAndView showRoleDetails(@RequestParam("js_id") String js_id){
    	int jsid=Integer.parseInt(js_id);
    	Map<String, Object> role=roleService.findSingleRole(jsid);
		ModelAndView mav=new ModelAndView();
		mav.setViewName("view/role/roleDetails");
		mav.addObject("role",role);
		return mav;
    }
   
    //查询所有
    @RequestMapping(INITURL + "/all")
    public ModelAndView initAll(){
        PageResults<?> pageResults=roleService.selectAllRole();
        
    	ModelAndView mav = new ModelAndView();
        mav.setViewName("/view/role/roleList");
        mav.addObject("allRole", pageResults.getResults());
        return mav;
    }
}
